<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="部门ID" prop="deptId">-->
<!--        <el-input-->
<!--          v-model="queryParams.deptId"-->
<!--          placeholder="请输入部门ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="动作ID" prop="actionId">-->
<!--        <el-input-->
<!--          v-model="queryParams.actionId"-->
<!--          placeholder="请输入动作ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="原设备" prop="originalDeviceId">
        <el-select v-model="queryParams.originalDeviceId" placeholder="请选择原设备" clearable>
          <el-option v-for="dict in deviceLists" :key="dict.id" :label="dict.deviceName"
                     :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="现设备" prop="nowDeviceId">
        <el-select v-model="queryParams.nowDeviceId" placeholder="请选择现设备" clearable>
          <el-option v-for="dict in deviceLists" :key="dict.id" :label="dict.deviceName"
                     :value="dict.id" />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="说明" prop="explain">-->
<!--        <el-input-->
<!--          v-model="queryParams.explain"-->
<!--          placeholder="请输入说明"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>

<!--    <div style="float: right">-->
<!--&lt;!&ndash;      <span class="frequency">刷新频率：</span>&ndash;&gt;-->
<!--      <el-dropdown size="small" split-button type="primary" class="dropdown" @command="handleCommand">-->
<!--        刷新频率：{{ selectedTime }}-->
<!--        <el-dropdown-menu slot="dropdown">-->
<!--          <el-dropdown-item v-for="time in timeOptions" :key="time" :command="time">{{ time }}</el-dropdown-item>-->
<!--        </el-dropdown-menu>-->
<!--      </el-dropdown>-->
<!--    </div>-->


    </el-form>

    <el-table v-loading="loading" :data="dispatchList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" :index="indexMethod"></el-table-column>
      <el-table-column label="焊缝编号" align="center" prop="actionId" :formatter="actionFormatter" />
      <el-table-column label="原设备" align="center" prop="originalDeviceId" :formatter="deviceFormatter1" width="180"/>
      <el-table-column label="现设备" align="center" prop="nowDeviceId" :formatter="deviceFormatter2" width="180"/>
      <el-table-column label="任务名称" align="center" prop="taskName" />
      <el-table-column label="原工单id" align="center" prop="originalWorkId" />
      <el-table-column label="现工单id" align="center" prop="nowWorkId" />
      <el-table-column label="原工序顺序" align="center" prop="originalProductionLineSequence" />
      <el-table-column label="现工序顺序" align="center" prop="nowProductionLineSequence" />
      <el-table-column label="原设备类型" align="center" prop="originalDeviceType" />
      <el-table-column label="现设备类型" align="center" prop="nowDeviceType" />
      <el-table-column label="当天调度批次" align="center" prop="dispatchNum" />
      <el-table-column label="当天总车序" align="center" prop="ji" />
      <el-table-column label="说明" align="center" prop="explain" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改动作调度对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="部门ID" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门ID" />
        </el-form-item>
        <el-form-item label="动作ID" prop="actionId">
          <el-input v-model="form.actionId" placeholder="请输入动作ID" />
        </el-form-item>
        <el-form-item label="原设备ID" prop="originalDeviceId">
          <el-input v-model="form.originalDeviceId" placeholder="请输入原设备ID" />
        </el-form-item>
        <el-form-item label="现设备ID" prop="nowDeviceId">
          <el-input v-model="form.nowDeviceId" placeholder="请输入现设备ID" />
        </el-form-item>
        <el-form-item label="说明" prop="explain">
          <el-input v-model="form.explain" placeholder="请输入说明" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDispatch, getDispatch, delDispatch, addDispatch, updateDispatch } from "@/api/zndd/action/dispatch";
import { listDevice } from '@/api/zndd/device/device'
import { listGroup } from '@/api/zndd/rest/group'
import { listAction } from '@/api/zndd/action/action'

export default {
  name: "Dispatch",
  data() {
    return {
      selectedTime: '5秒钟',
      allTimes: ['5秒钟', '10秒钟', '20秒钟', '30秒钟', '40秒钟', '50秒钟'],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 动作调度表格数据
      dispatchList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        actionId: null,
        originalDeviceId: null,
        nowDeviceId: null,
        explain: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      deviceLists:[],
      actionList:[],
    };
  },
  created() {
    this.getDeviceInfo();
    this.getList();
    var param = {pageNum:1,pageSize:500}
    //获取动作数据
    listAction(param).then(response => {
      this.actionList = response.rows;
    });
  },
  methods: {
    //格式化动作
    actionFormatter(row) {
      var lt = null;
      this.actionList.forEach(action => {
        if(action.id == row.actionId){
          lt = action;
        }
      });
      return lt.weldNo+" ("+lt.weldZoneX +"-"+lt.weldZoneY +")";
    },
    getDeviceInfo(){
      listDevice({pageNum: 1, pageSize: 50}).then(response => {
        this.deviceLists = response.rows;
      });
    },
    deviceFormatter1(row){
      var lt = null;
      this.deviceLists.forEach(device => {
        if(device.id == row.originalDeviceId){
          lt = device;
        }
      });
      if(lt == null){
        return "";
      }else{
        return lt.deviceName;
      }
    },
    deviceFormatter2(row){
      var lt = null;
      this.deviceLists.forEach(device => {
        if(device.id == row.nowDeviceId){
          lt = device;
        }
      });
      if(lt == null){
        return "";
      }else{
        return lt.deviceName;
      }
    },
    /** 序号 */
    indexMethod(index) {
      let pageNum = this.queryParams.pageNum - 1;
      if (pageNum !== -1 && pageNum !== 0) {
        return (index + 1) + (pageNum * this.queryParams.pageSize);
      } else {
        return index + 1;
      }
    },
    /** 查询动作调度列表 */
    getList() {
      this.loading = true;
      listDispatch(this.queryParams).then(response => {
        this.dispatchList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        actionId: null,
        originalDeviceId: null,
        nowDeviceId: null,
        explain: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加动作调度";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDispatch(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改动作调度";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDispatch(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDispatch(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除动作调度编号为"' + ids + '"的数据项？').then(function() {
        return delDispatch(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('action/dispatch/export', {
        ...this.queryParams
      }, `dispatch_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
